'''
Author: LHY 2601958675@qq.com
Date: 2023-07-11 15:46:20
LastEditors: LHY
LastEditTime: 2023-07-11 17:07:16
Description: 图片抓取
'''
from bs4 import BeautifulSoup
import requests
import datetime
import os
name_test = 1 
domain = "https://umei.net"
"""
子页面URL 如果开头是/ 直接拼接域名即可

如果不是 /开头 此时需要找到上一级别的url  去除最后的/后面的东西  然后直接拼接这个 东西

"""


def catchList():
    url = "https://umei.net/e/search/result/?searchid=7785"
    result = requests.get(url)
    # print(result.text)

    beautiful = BeautifulSoup(result.text,"html.parser")
    a_list = beautiful.find_all("li",attrs={"class":"i_list list_n2"})
    # print(a_list)

    for item in a_list:
        a = item.find("a")
        item_href = a.get("href")
        xxx = f"{item_href}"
        catchDetailPage(xxx)
        pass
    pass


def catchDetailPage(sbUrl):
    detailUrl = f"{domain}{sbUrl}" # "https://umei.net/tupian/54895.html"
    detail_result = requests.get(detailUrl)
    detail_result.encoding = 'utf-8'
    # print("detail",detail_result.text)
    child_bs = BeautifulSoup(detail_result.text,"html.parser")
    div = child_bs.find("div",attrs={"class":"image_div"})
    img = div.find("img")
    image_url = img.get("src")
    # print(image_url)
    #下载图片
    imageRespon = requests.get(image_url)
    nameImage = datetime.datetime.now().time()
    print(datetime.datetime.now().time())
    with open(f"{nameImage}.jpg",mode="wb") as f:
        f.write(imageRespon.content)
    pass

# detailUrl = "https://umei.net/tupian/54895.html"
# detail_result = requests.get(detailUrl)
# detail_result.encoding = 'utf-8'
# print("detail",detail_result.text)


catchList()